#ifndef APP_CONTEXT_H_
#define APP_CONTEXT_H_

#include <string>
#include <map>
#include <mutex>
#include <list>
#include "com/properties.h"
#include "com/online_table.h"

/// @brief 应用全局上下文
class AppContext
{
public:
    static AppContext *Get()
    {
        return instance_;
    }

    void set_client_timeout(int tv)
    {
        this->client_timeout_ = tv;
    }

    int client_timeout() const
    {
        return this->client_timeout_;
    }

    void set_server_pub_key(std::string pub_key)
    {
        this->server_pub_key_ = pub_key;
    }

    std::string server_pub_key() const
    {
        return this->server_pub_key_;
    }

    void set_server_prv_key(std::string prv_key)
    {
        this->server_prv_key_ = prv_key;
    }

    std::string server_prv_key() const
    {
        return this->server_prv_key_;
    }

    /// @brief 加载别名表
    /// @param alias_table_prop 别名表的默认路径
    void LoadAliasTable(std::string alias_table_prop = "./alias_table.properties");

    /// @brief 根据用户名获取用户别名，如果当前缓存中不存在则重新加载一次文件
    /// @param key 用户名
    std::string GetAndReloadAlias(std::string key);

    void RegistService(std::string service_name);

    /// @brief 根据别名寻找用户名
    /// @param alias 用户别名通常应该由管理员指派
    std::string GetUsername(std::string alias);

    /// @brief 用户上线
    /// @param user_id 用户ID
    /// @param user_alias 用户别名
    bool UserOnLine(unsigned int user_id, std::string user_alias);

    /// @brief 用户离线
    /// @param user_id 用户ID
    void UserOffLine(unsigned int user_id);

    /// @brief 用户在线表
    std::map<unsigned int, std::string> online_table();

private:
    AppContext()
    {
        this->online_table_ = new OnlineTable;
    }
    static AppContext *instance_;
    int client_timeout_ = 30;
    std::string server_pub_key_ = "";
    std::string server_prv_key_ = "";
    std::string alias_table_prop_ = "";
    std::mutex ctx_mtx_;
    OnlineTable *online_table_;
};
#endif